import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import vueDevTools from 'vite-plugin-vue-devtools'
import autoImport from 'unplugin-auto-import/vite'
import components from 'unplugin-vue-components/vite'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import setupExtend from 'vite-plugin-vue-setup-extend'
//  ElementPlus的Icon自动导入
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
import path from 'path'

export default defineConfig({
  base: './',
  plugins: [
    vue({
      template: {
        transformAssetUrls: {
          video: ['src', 'poster'],
          source: ['src'],
          img: ['src'],
          image: ['xlink:href', 'href'],
          use: ['xlink:href', 'href'],
          // 添加对自定义的组件路径别名解析
          'svg-icon': ['src'],
        },
      },
    }),
    vueJsx(),
    // vueDevTools(),
    setupExtend(),
    autoImport({
      imports: ['vue', 'vue-router', 'pinia'],
      dts: false,
      eslintrc: {
        enabled: true,
      },
      //element
      resolvers: [
        IconsResolver({
          prefix: 'Icon',
        }),
      ],
    }),
    components({
      dirs: ['src/components'],
      extensions: ['vue'],
      dts: false,
      include: [/.vue$/, /.vue?vue/],
      resolvers: [
        IconsResolver({
          enabledCollections: ['ep'],
        }),
      ],
    }),
    createSvgIconsPlugin({
      iconDirs: [path.resolve(process.cwd(), 'src/assets/icon')],
      symbolId: 'icon-[dir]-[name]',
    }),
    // 自动安装图标
    Icons({
      autoInstall: true,
    }),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url)),
    },
  },
  server: {
    port: 8023,
    host: true,
    open: true,
    proxy: {
      '/dev-api': {
        //线上地址
        target: 'http://111.202.231.146:8016/feedback/prod-api',
        //本地
        // target: 'http://192.168.11.248:8083',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/dev-api/, ''),
      },
      '/feedback/prod-api': {
        //线上地址
        target: 'http://111.202.231.146:8016/feedback/prod-api',
        changeOrigin: true,
      },
    },
  },
})
